package com.go.where.family.component.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;

import com.go.where.family.component.CourseComponent;
import com.go.where.family.constants.Constant;
import com.go.where.family.constants.TransmissionCode;
import com.go.where.family.exception.ServerDisponseException;
import com.go.where.family.pojo.CourseInfo;
import com.go.where.family.pojo.CourseRetrospectInfo;
import com.go.where.family.scoket.Head;
import com.go.where.family.service.CourseRetrospectService;
import com.go.where.family.service.CourseService;
import com.go.where.family.utils.CommonUtil;
import com.go.where.family.vo.CourseVo;
import com.google.gson.Gson;

@Component("courseComponent")
public class CourseComponentImpl implements CourseComponent {

	private static final Logger log = Logger
			.getLogger(CourseComponentImpl.class);
	private Gson gson;
	@Resource(name = "courseService")
	private CourseService courseService;
	@Resource(name = "courseRetrospectService")
	private CourseRetrospectService courseRetrospectService;

	public CourseComponentImpl() {
		gson = CommonUtil.gson();
	}

	@Override
	public List<CourseInfo> queryList(String body, Head head) {
		CourseVo vo = gson.fromJson(body, CourseVo.class);
		if (vo != null) {
			Integer currentPage = vo.getPageIndex();
			Integer pageSize = vo.getPageCount();
			Integer pageStart = 1;
			Integer pageEnd = 10;
			if (currentPage != null && pageSize != null) {
				pageStart = (currentPage - 1) * pageSize + 1;
				pageEnd = currentPage * pageSize;
			}
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("pageStart", pageStart);
			map.put("pageEnd", pageEnd);
			map.put("isDelete", Constant.IS_NOT_DELETE);
			// map.put("month", vo.getMonth());
			if (Constant.ORDER_TYPE_ASC.equals(vo.getOrderType())) {
				map.put("page_orderByAsc", vo.getOrderBy());
			} else {
				map.put("page_orderByDesc", vo.getOrderBy());
			}

			List<CourseInfo> list = courseService.queryList(map);
			if (list != null && !list.isEmpty()) {
				for (CourseInfo c : list) {
					if (StringUtils.isNotBlank(c.getCoursePhoto())) {
						c.setCoursePhoto(CommonUtil.getUrlPrefix()
								+ c.getCoursePhoto());
					}
				}
				return list;
			} else {
				return new ArrayList<CourseInfo>();
			}
		} else {
			log.info("requestId:" + head.getRequestId() + ",获取课程列表信息时，报文解析异常。");
			throw new ServerDisponseException(TransmissionCode.ERROR_ADJUST
					.getCode(), TransmissionCode.ERROR_ADJUST.getMessage());
		}
	}

	@Override
	public CourseInfo queryCourseDetail(String body, Head head) {
		CourseVo vo = gson.fromJson(body, CourseVo.class);
		if (vo != null) {
			String courseId = vo.getCourseId();
			CourseInfo vv = courseService.queryCourseDetail(courseId);
			if (vv != null) {
				return vv;
			} else {
				log.info("requestId:" + head.getRequestId()
						+ ",查询课题详情信息时,没有查询到数据,课程ID：" + courseId);
				throw new ServerDisponseException(TransmissionCode.NO_DATA
						.getCode(), TransmissionCode.NO_DATA.getMessage());
			}
		} else {
			log.info("requestId:" + head.getRequestId() + ",获取课程详情信息时，报文解析异常");
			throw new ServerDisponseException(TransmissionCode.ERROR_ADJUST
					.getCode(), TransmissionCode.ERROR_ADJUST.getMessage());
		}
	}

	@Override
	public List<CourseRetrospectInfo> queryRetroList(String body, Head head) {
		CourseVo vo = gson.fromJson(body, CourseVo.class);
		if (vo != null) {
			Integer currentPage = vo.getPageIndex();
			Integer pageSize = vo.getPageCount();
			Integer pageStart = 1;
			Integer pageEnd = 10;
			if (currentPage != null && pageSize != null) {
				pageStart = (currentPage - 1) * pageSize + 1;
				pageEnd = currentPage * pageSize;
			}
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("pageStart", pageStart);
			map.put("pageEnd", pageEnd);
			map.put("courseId", vo.getCourseId());
			map.put("courseName", vo.getCourseName());
			map.put("isDelete", Constant.IS_NOT_DELETE);
			if (StringUtils.isNotBlank(vo.getOrderBy())) {
				if (Constant.ORDER_TYPE_ASC.equals(vo.getOrderType())) {
					if ("reserves".equals(vo.getOrderBy())) {
						map.put("page_orderByAsc", vo.getOrderBy());
					} else {
						map.put("page_orderByAsc", "cr." + vo.getOrderBy());
					}
				} else {
					if ("reserves".equals(vo.getOrderBy())) {
						map.put("page_orderByDesc", vo.getOrderBy());
					} else {
						map.put("page_orderByDesc", "cr." + vo.getOrderBy());
					}
				}
			}

			List<CourseRetrospectInfo> list = courseRetrospectService
					.queryList(map);
			if (list != null && !list.isEmpty()) {
				for (CourseRetrospectInfo c : list) {
					if (StringUtils.isNotBlank(c.getCoursePhoto())) {
						c.setCoursePhoto(CommonUtil.getUrlPrefix()
								+ c.getCoursePhoto());
					}
				}
				return list;
			} else {
				return new ArrayList<CourseRetrospectInfo>();
			}
		} else {
			log.info("requestId:" + head.getRequestId()
					+ ",获取课程回顾列表信息时，报文解析异常。");
			throw new ServerDisponseException(TransmissionCode.ERROR_ADJUST
					.getCode(), TransmissionCode.ERROR_ADJUST.getMessage());
		}
	}

	@Override
	public List<CourseVo> queryCourses(String body, Head head) {
		CourseVo vo = gson.fromJson(body, CourseVo.class);
		if (vo != null) {
			List<CourseInfo> cvoList = courseService.queryGroupByBeginTime();
			List<CourseVo> courseVoList = new ArrayList<CourseVo>();
			if (cvoList != null) {
				for (CourseInfo cInfo : cvoList) {
					CourseVo cVo = new CourseVo();
					cVo.setMonth(cInfo.getMonth());
					Map<String, Object> map = new HashMap<String, Object>();
					map.put("month", cInfo.getMonth());
					map.put("isDelete", Constant.IS_NOT_DELETE);
					List<CourseInfo> courseList = courseService
							.queryMonthCourseDetail(map);
					if (courseList != null && !courseList.isEmpty()) {
						for (CourseInfo c : courseList) {
							if (StringUtils.isNotBlank(c.getCoursePhoto())) {
								c.setCoursePhoto(CommonUtil.getUrlPrefix()
										+ c.getCoursePhoto());
							}
						}
					}
					cVo.setCourseList(courseList);
					courseVoList.add(cVo);
				}
				if (courseVoList.size() > 0) {
					return courseVoList;
				} else {
					throw new ServerDisponseException(TransmissionCode.NO_DATA
							.getCode(), TransmissionCode.NO_DATA.getMessage());
				}
			}
		} else {
			log.info("requestId:" + head.getRequestId() + ",获取课程排期信息时，报文解析异常。");
			throw new ServerDisponseException(TransmissionCode.ERROR_ADJUST
					.getCode(), TransmissionCode.ERROR_ADJUST.getMessage());
		}
		return null;
	}

	@Override
	public boolean update(String body, Head head) {
		CourseVo vo = gson.fromJson(body, CourseVo.class);
		if (vo != null) {
			CourseInfo act = courseService.queryCourseDetail(vo.getCourseId());
			if (act != null) {
				Integer likeNum = act.getLikeNum();
				if (StringUtils.isNotBlank(vo.getLikeOrNot())) {
					if ("1".equals(vo.getLikeOrNot())) {
						likeNum = CommonUtil.add(likeNum, 1);
					} else if ("0".equals(vo.getLikeOrNot())) {
						likeNum = CommonUtil.sub(likeNum, 1);
					}
				}
				CourseInfo info = new CourseInfo();
				info.setCourseId(vo.getCourseId());
				info.setLikeNum(likeNum);
				int result = courseService.update(info);
				if (result > 0) {
					return true;
				} else {
					log.info("requestId:" + head.getRequestId()
							+ ",更新课程点赞数时，更新异常。课程Id：" + vo.getCourseId());
					return false;
				}
			} else {
				log.info("requestId:" + head.getRequestId()
						+ ",更新课程点赞量时,没有查询到数据,课程ID：" + vo.getCourseId());
				throw new ServerDisponseException(TransmissionCode.NO_DATA
						.getCode(), TransmissionCode.NO_DATA.getMessage());
			}
		} else {
			log.info("requestId:" + head.getRequestId() + ",更新课程信息时，参数报文解析异常。");
			throw new ServerDisponseException(TransmissionCode.ERROR_ADJUST
					.getCode(), TransmissionCode.ERROR_ADJUST.getMessage());
		}
	}

}
